package com.flyaudio.xiaopengautopilot.activity;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyaudio.xiaopengautopilot.R;
import com.flyaudio.xiaopengautopilot.constant.JSONKeyName;
import com.flyaudio.xiaopengautopilot.custom.SectorProgressView;
import com.flyaudio.xiaopengautopilot.presenter.IActivityCallback;
import com.flyaudio.xiaopengautopilot.presenter.IVehicleModule;
import com.flyaudio.xiaopengautopilot.util.DataInfo;
import com.flyaudio.xiaopengautopilot.util.JSONUtil;
import com.flyaudio.xiaopengautopilot.util.L;
import com.flyaudio.xiaopengautopilot.constant.ModuleKey;
import com.flyaudio.xiaopengautopilot.constant.MsgViewUtil;
import com.flyaudio.xiaopengautopilot.util.MySharedPreferences;
import com.flyaudio.xiaopengautopilot.view.AvgPowerConsumptionDialog;
import com.flyaudio.xiaopengautopilot.view.ConditionDialog;
import com.flyaudio.xiaopengautopilot.view.MileageView;
import com.flyaudio.xiaopengautopilot.view.PowerConsumptionBarChart;
import com.flyaudio.xiaopengautopilot.view.PowerConsumptionView;
import com.flyaudio.xiaopengautopilot.view.TpmsDialog;

public class MainActivity extends Activity implements View.OnClickListener, View.OnTouchListener, IActivityCallback {
    private final String TAG = "MainActivity";

    private MileageView mMileageView;
    private PowerConsumptionView mPowerConsumptionView;
    private HorizontalScrollView hs_mileage;
    private final int MileageViewWidth = 1000;
    private int mMileageViewPageIndex = 0;
    private int MileageViewPageX[] = {0, 1040 - 20, 1080 + 1040 - 20};
    private PowerConsumptionBarChart mPowerConsumptionBarChart;

    private Button vehicle_condition_bt, vehicle_tpms_bt, vehicle_power_consumption_bt;
    private SectorProgressView vehicle_power_spv;
    private TextView vehicle_recharge_mileage_tv;
    private Button vehicle_tips_bt;

    private final int initMain = 0x1000;
    private ConditionDialog conditionDialog;
    private AvgPowerConsumptionDialog apcDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().setStatusBarColor(getResources().getColor(R.color.colorVehicheStatusBarColor));
        }
        setContentView(R.layout.activity_main);
        initView();

        MySharedPreferences.attachContext(MainActivity.this);
        handler.sendEmptyMessage(initMain);

        L.d(TAG,"onCreate");
    }

    @Override
    protected void onPause() {
        super.onPause();
        IVehicleModule.getInstance().setCallBack(null);

        L.d(TAG, "onPause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        L.d(TAG,"onDestroy");
    }

    @Override
    protected void onResume() {
        super.onResume();
        IVehicleModule.getInstance().setCallBack(this);
        updateView();
        L.d(TAG,"onResume");
    }

    private void initView() {
        vehicle_recharge_mileage_tv = findViewById(R.id.vehicle_recharge_mileage_tv);
        vehicle_power_spv = findViewById(R.id.vehicle_power_spv);
        vehicle_condition_bt = findViewById(R.id.vehicle_condition_bt);
        vehicle_tpms_bt = findViewById(R.id.vehicle_tpms_bt);
        vehicle_power_consumption_bt = findViewById(R.id.vehicle_power_consumption_bt);
        hs_mileage = findViewById(R.id.hs_mileage);
        vehicle_tips_bt = findViewById(R.id.vehicle_tips_bt);

        vehicle_tips_bt.setOnClickListener(this);
        vehicle_condition_bt.setOnClickListener(this);
        vehicle_tpms_bt.setOnClickListener(this);
        vehicle_power_consumption_bt.setOnClickListener(this);
        hs_mileage.setOnTouchListener(this);
    }

    private void initMileageView() {
        LinearLayout linearLayout = findViewById(R.id.hl_mileageview_container);
        mMileageView = new MileageView(this);
       // mPowerConsumptionView = new PowerConsumptionView(this);
        mPowerConsumptionBarChart = new PowerConsumptionBarChart(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(MileageViewWidth, 565);
        layoutParams.setMargins(40, 0, 0, 0);
        linearLayout.addView(mMileageView.getView(), layoutParams);
//        layoutParams = new LinearLayout.LayoutParams(MileageViewWidth, 565);
//        layoutParams.setMargins(20, 0, 0, 0);
//        linearLayout.addView(mPowerConsumptionView.getView(), layoutParams);
        layoutParams = new LinearLayout.LayoutParams(MileageViewWidth, 565);
        layoutParams.setMargins(20, 0, 40, 0);
        linearLayout.addView(mPowerConsumptionBarChart.getView(), layoutParams);
    }

    private final int set_mileage_page = 1;
    private void dealHandler(Message msg){
        switch (msg.what) {
            case set_mileage_page: //smoothScrollTo 需要使用handler才有效
                hs_mileage.smoothScrollTo(MileageViewPageX[mMileageViewPageIndex], 0);
                L.d(TAG,"onTouch mMileageViewPageIndex " + mMileageViewPageIndex);
                break;
            case initMain:
                initMileageView();
                break;
            case MsgViewUtil.Update_Mileage_View:
                mMileageView.update();
                break;
            case MsgViewUtil.Update_Daily_Mode:
                vehicle_power_spv.setThumbLimitMode(false);
                MySharedPreferences.setPowerChargingLimitMode(false);
                break;
            case MsgViewUtil.Update_Long_Distance_Mode:
                vehicle_power_spv.setThumbLimitMode(true);
                MySharedPreferences.setPowerChargingLimitMode(true);
                break;
            case MsgViewUtil.Update_Endurance_Mileage:
                setEnduranceMileageView();
                break;
            case MsgViewUtil.Update_Soc:
                setSocView();
                break;
            case MsgViewUtil.Update_KWh_AVG:
                //mPowerConsumptionView.update();
                if(apcDialog != null && apcDialog.isShowing()) {
                    apcDialog.update();
                }
                break;
            case MsgViewUtil.Update_Condition_Dialog:
                if(conditionDialog != null && conditionDialog.isShowing()) {
                    conditionDialog.updateView();
                }
                 break;
        }
    }
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            dealHandler(msg);
            super.handleMessage(msg);
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.vehicle_condition_bt:
                conditionDialog = new ConditionDialog(this, R.style.setDialog, R.layout.dialog_condition);
                conditionDialog.show();
                break;
            case R.id.vehicle_tpms_bt:
                TpmsDialog tpmsDialog = new TpmsDialog(this, R.style.setDialog, R.layout.dialog_tpms);
                tpmsDialog.show();
                break;
            case R.id.vehicle_power_consumption_bt:
                apcDialog = new AvgPowerConsumptionDialog(this, R.style.setDialog, R.layout.dialog_avg_power_consumption);
                apcDialog.show();
                break;
            case R.id.vehicle_tips_bt:
                break;
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (view.getId()) {
            case R.id.hs_mileage:
                float sensitivity = 0.2f;
//                Log.d("onTouch"," hs_mileage.getScrollX() " + hs_mileage.getScrollX());
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    //当前view滑动了width的1/5则切到下一个view
                    int x = hs_mileage.getScrollX();
                    float pre = MileageViewPageX[mMileageViewPageIndex] - MileageViewWidth * sensitivity;
                    float next = MileageViewPageX[mMileageViewPageIndex] + MileageViewWidth * sensitivity;
                    if (x < pre) {
                        mMileageViewPageIndex--;
                    } else if (x > next) {
                        mMileageViewPageIndex++;
                    }
                    handler.sendEmptyMessage(set_mileage_page);
                }
                break;
        }
        return false;
    }

    @Override
    public void sendMsg(int what, Object arg) {
        Message msg = handler.obtainMessage(what);
        msg.obj = arg;
        handler.sendMessage(msg);
    }

    private void setEnduranceMileageView() {
        int mEnduranceMileage =  (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.EnduranceMileage, 0);
//        if(DataInfo.getInstance().getDataParamerInfo(ModuleKey.BMS_EnduranceMileage) != null) {
//            mEnduranceMileage = (int) DataInfo.getInstance().getDataParamerInfo(ModuleKey.BMS_EnduranceMileage);
//        }
        vehicle_recharge_mileage_tv.setText(mEnduranceMileage+"");
    }

    private void setSocView() {
        int mSoc = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.BmsSoc, 0);
//        if(DataInfo.getInstance().getDataParamerInfo(ModuleKey.BMS_SOC) != null) {
//            mSoc = (int) DataInfo.getInstance().getDataParamerInfo(ModuleKey.BMS_SOC);
//        }
        vehicle_power_spv.setProgress(mSoc);
    }

    private void updateView() {
        vehicle_power_spv.setThumbLimitMode(MySharedPreferences.getPowerChargingLimitMode());
        setEnduranceMileageView();
        setSocView();

        if(mMileageView != null) {
            mMileageView.update();
        }
//        if(mPowerConsumptionView != null) {
//            mPowerConsumptionView.update();
//        }
    }


}
